package com.hx.er.service.admin.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.hx.er.bean.base.AnnouncementBean;
import com.hx.er.bean.base.RestraurantBean;
import com.hx.er.bean.base.UserBean;
import com.hx.er.dao.IContactDao;
import com.hx.er.dao.IRestraurantDao;
import com.hx.er.dao.IUserDao;
import com.hx.er.service.admin.IAdminRestService;
import com.hx.er.service.base.ErBaseService;

public class AdminRestServiceImpl extends ErBaseService implements IAdminRestService {

	@Autowired
	private IRestraurantDao restraurantDao;

	@Autowired
	private IContactDao contactDao;

	@Autowired
	private IUserDao userDao;

	@Override
	public List<RestraurantBean> getRestList(Map<String, Object> conditions) {
		return this.restraurantDao.getRestList(conditions);
	}

	@Override
	public int getRestCount(Map<String, Object> conditions) {
		return this.restraurantDao.getRestCount(conditions);
	}

	@Override
	public RestraurantBean getRestById(long restId) {
		return this.restraurantDao.getRestById(restId);
	}

	@Override
	public AnnouncementBean getAnnAliveByRestId(long restId) {
		return this.contactDao.getAnnAliveByRestId(restId);
	}

	@Override
	public int deleteRest(RestraurantBean restBean) {
		return this.restraurantDao.deleteRest(restBean);
	}

	@Override
	public int reviveRest(RestraurantBean restBean) {
		return this.restraurantDao.reviveRest(restBean);
	}

	@Override
	public int updateRest(RestraurantBean restBean) {
		return this.restraurantDao.updateRest(restBean);
	}

	@Override
	public UserBean getUserAliveById(long userId) {
		return this.userDao.getUserAliveById(userId);
	}

	@Override
	@Transactional
	public Map<String, Boolean> insertRestWithoutId(RestraurantBean restBean, UserBean userBean) {
		// 检查餐厅名称重复
		int count = this.restraurantDao.checkServerName(restBean);

		// 用户名、邮箱、手机号码冲突
		int countUserName = this.userDao.checkUserName(userBean);

		// 初始化返回结果
		Map<String, Boolean> msgMap = new HashMap<String, Boolean>();
		if (countUserName > 0) {
			msgMap.put("msg.userName", true);
		}
		if (count > 0) {
			msgMap.put("msg.serverName", true);
		}

		// 如果map中是空，则插入后返回，如果map中有错误信息，则直接返回
		if (msgMap.isEmpty()) {
			// 插入餐厅
			this.restraurantDao.insertRestWithoutId(restBean);
			// 获取刚刚插入的餐馆数据
			RestraurantBean newestRestBean = this.restraurantDao.getNewRest();
			// 设置餐馆ID
			userBean.setRestId(newestRestBean.getRestId());
			// 插入用户
			this.userDao.insertCustomerWithoutId(userBean);
		}
		
		return msgMap;
	}

	@Override
	public AnnouncementBean getAnnAlive(AnnouncementBean announcementBean) {
		return this.contactDao.getAnnAlive(announcementBean);
	}

	@Override
	public UserBean getManagerByRestId(long restId) {
		return this.userDao.getManagerByRestId(restId);
	}
}
